package com.itcast.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itcast.mapper.MemberMapper;
import com.itcast.pojo.Member;
import com.itcast.service.MemberService;
import com.itcast.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberMapper memberMapper;
    @Override
    public Member findMember(String telephone) {
        return memberMapper.findByTelephone(telephone);
    }

    @Override
    public void add(Member member) {
        String password = member.getPassword();
        if (password!=null){
            //将密码进行加密
            String md5 = MD5Utils.md5(password);
            member.setPassword(md5);
        }
        memberMapper.add(member);
    }

    //根据月份查询会员数据
    @Override
    public List<Integer> findByMonthMember(List<String> months) {
        List<Integer> memberCount = new ArrayList<>();
        for (String month : months) {
            String date = month + ".31";
            Integer count = memberMapper.findMemberCountBeforeDate(date);
            memberCount.add(count);
        }
        return memberCount;
    }
}
